<?php
namespace app\api\controller\xilumarket;


use app\common\controller\Api;
use app\common\model\xilumarket\UserAccount;
use app\common\model\xilumarket\UserMessage;
use think\Db;
use think\db\Expression;
use think\Exception;
use think\exception\PDOException;

class Message extends Api {

    protected $noNeedRight = ['*'];
    /**
     * @ApiTitle (消息概况)
     * @ApiSummary (消息总的统计情况)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilumarket.message/summary)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function summary() {
        $user_account = UserAccount::where('user_id',$this->auth->id)->find();

        $systemMessage = UserMessage::where('module_type', UserMessage::MODULE_TYPE_SYSTEM)
            ->where('user_id',$this->auth->id)
            ->order('id', 'desc')
            ->find();

        $personalMessage = UserMessage::where('module_type',UserMessage::MODULE_TYPE_INTERACTION)
            ->where('user_id',$this->auth->id)
            ->order("id",'desc')
            ->find();
        $data = [
            'system'=>['total'=>$user_account->system_message,'message'=>$systemMessage],
            'interaction'=>['total'=>$user_account->user_message,'message'=>$personalMessage]
        ];
        $this->success('查询成功',$data);
    }

    /**
     * @ApiTitle (互动消息)
     * @ApiSummary (互动消息)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.message/personal_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function interaction_list() {
//        $user_account = UserAccount::addAccount($this->auth->id);
        //如果有消息
//        if($user_account->user_message>0) {
//            $user_account->save(['user_message'=>0]);
//            UserMessage::where('module_type', UserMessage::MODULE_TYPE_INTERACTION)
//                ->where('user_id',$this->auth->id)
//                ->order('id','desc')
//                ->update(['read'=>1]);
//        }
        $list = UserMessage::where('module_type', UserMessage::MODULE_TYPE_INTERACTION)
            ->where('user_id',$this->auth->id)
            ->order('id','desc')
            ->paginate(20);

        $this->success('查询成功', $list);
    }

    /**
     * @ApiTitle (系统通知)
     * @ApiSummary (系统通知)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.message/system_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function system_list() {
//        $user_account = UserAccount::addAccount($this->auth->id);
        //如果有消息
//        if($user_account->system_message>0) {
//            $user_account->save(['system_message'=>0]);
//            UserMessage::where('module_type', UserMessage::MODULE_TYPE_SYSTEM)
//                ->where('user_id',$this->auth->id)
//                ->order('id','desc')
//                ->update(['read'=>1]);
//        }
        $list = UserMessage::where('module_type', UserMessage::MODULE_TYPE_SYSTEM)
            ->where('user_id',$this->auth->id)
            ->order('id','desc')
            ->paginate(20);
        $this->success('查询成功', $list);
    }

    /**
     * 点击变成已读
     */
    public function set_read(){
        $id = $this->request->param('message_id');
        $type = $this->request->param('type');
        $message = $id?UserMessage::get($id):null;
        if(!$message){
            $this->error("消息未找到");
        }
        $field = $type == 'system'? 'system_message' : 'user_message';
        if($message->read == 0){
            Db::startTrans();
            try {
                $user_account = UserAccount::addAccount($this->auth->id);
                $message->read = 1;
                $message->read_time = time();
                $message->save();
                if($user_account->$field>0){
                    $user_account->$field = new Expression($field.'-1');
                    $user_account->save();
                }
            }catch (Exception|PDOException $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }
        Db::commit();
        $this->success('已读成功');


    }


}